package com.lgt_enterprise_android.view.pages.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSONObject;
import com.lgt_enterprise_android.Constant;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.HttpManager;
import com.lgt_enterprise_android.utils.PrefUtils;
import com.lgt_enterprise_android.utils.StringUtil;
import com.lgt_enterprise_android.view.pages.LoginActivity;

public class EditPasswordActivity extends BaseActivity {

    private View viewOld;
    private EditText edOldPwd,edNewPwd,edNewPwdConfirm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_password);

        viewOld=findViewById(R.id.frameOld);

        edOldPwd=(EditText)findViewById(R.id.edit_old_pwd);
        edNewPwd=(EditText)findViewById(R.id.edit_new_pwd);
        edNewPwdConfirm=(EditText)findViewById(R.id.edit_new_pwd_confirm);

        HttpManager.getProfileInfo(mContext, new BasicHttp.IMyCallBack() {
            @Override
            public void onResponse(Object resonseObj) {
                JSONObject jo=(JSONObject)resonseObj;
                if(jo.getBooleanValue("existPwd")){
                    viewOld.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(String mes) {

            }
        });

        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(viewOld.getVisibility()==View.VISIBLE){
                    if(StringUtil.isEmpty(edOldPwd.getText().toString().trim())){
                        showToast("旧密码不能为空");
                        return;
                    }
                }

                if(StringUtil.isEmpty(edNewPwd.getText().toString().trim())){
                    showToast("新密码不能为空");
                    return;
                }

                if(!edNewPwd.getText().toString().trim().equals(edNewPwdConfirm.getText().toString().trim())){
                    showToast("新密码二次输入的不一致");
                    return;
                }

                HttpManager.editPwd(mContext, edOldPwd.getText().toString().trim(), edNewPwd.getText().toString().trim(), new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {
                        showToast("密码修改成功");
                        Intent intent = new Intent(mContext, LoginActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        Constant.token = "";
                        PrefUtils.setSharedPrefStringData(mContext, PrefUtils.TOKEN, "");
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onFailure(String mes) {

                    }
                });
            }
        });
    }
}
